//
//  SDNetworkTool.swift
//  Seed
//
//  Created by Samuel on 2017/6/6.
//  Copyright © 2017年 RedSoft. All rights reserved.
//

import UIKit
import SVProgressHUD
import Alamofire
import SwiftyJSON

class SDNetworkTool: NSObject {
    //单例
    static let shared = SDNetworkTool()
    //获取验证码
    func getverifyCode(withPhoneNum num:String, target:String, finished:@escaping(() -> ())) {
        let url = BASE_URL + "sms_code"
        let params = ["tel": num,
                      "target": target,
                      ] as [String : Any]
        Alamofire
            .request(url, parameters: params)
            .responseJSON { (response) in
                guard response.result.isSuccess else {
                    SVProgressHUD.showError(withStatus: "加载失败...")
                    return
                }
                if let value = response.result.value {
                    printLog(response.result.value)
                    let dict = JSON(value)
                    let code = dict["statusCode"].intValue
                    let message = dict["statusMsg"].stringValue
                    guard code == RETURN_OK else {
                        SVProgressHUD.showInfo(withStatus: message)
                        return
                    }
                    finished()
                }
        }
    }
    //注册新用户
    func register(withPhone phone:String, passwd:String, unionId:String?, smsCode:String, nickname:String, headImage:String, finished:@escaping((_ account:SDAccount) -> ())) {
        let url = BASE_URL + "register"
        let params = ["tel": phone,
                      "password": passwd,
                      "wechat_union_id": unionId ?? "",
                      "sms_code":smsCode,
                      "nickname":nickname,
                      "head_image":headImage
                      ] as [String : Any]
        Alamofire
            .request(url, method:.post, parameters: params)
            .responseJSON { (response) in
                guard response.result.isSuccess else {
                    SVProgressHUD.showError(withStatus: "加载失败...")
                    return
                }
                if let value = response.result.value {
                    printLog(response.result.value)
                    let dict = JSON(value)
                    let code = dict["statusCode"].intValue
                    let message = dict["statusMsg"].stringValue
                    guard code == RETURN_OK else {
                        SVProgressHUD.showInfo(withStatus: message)
                        return
                    }
                    SVProgressHUD.dismiss()
                    if let data = dict["data"].dictionaryObject{
                        let account = SDAccount(dict: data as [String:AnyObject])
                        finished(account)
                    }
                }
        }
    }
    //手机号登陆
    func login(withPhone phone:String, passwd:String, finished:@escaping((_ account:SDAccount) -> ())){
        let url = BASE_URL + "login/tel"
        let params = ["tel": phone,
                      "password": passwd,
            ] as [String : Any]
        Alamofire
            .request(url, method:.post, parameters: params)
            .responseJSON { (response) in
                guard response.result.isSuccess else {
                    SVProgressHUD.showError(withStatus: "加载失败...")
                    return
                }
                if let value = response.result.value {
                    printLog(response.result.value)
                    let dict = JSON(value)
                    let code = dict["statusCode"].intValue
                    let message = dict["statusMsg"].stringValue
                    guard code == RETURN_OK else {
                        SVProgressHUD.showInfo(withStatus: message)
                        return
                    }
                    SVProgressHUD.dismiss()
                    if let data = dict["data"].dictionaryObject{
                        let account = SDAccount(dict: data as [String:AnyObject])
                        finished(account)
                    }
                    
                }
        }
    }
    //微信登录
    func weixinLogin(withUnionId unionId:String, success:@escaping((_ account:SDAccount) -> ()), failure:@escaping (() -> ()) ) {
        let url = BASE_URL + "login/wechat"
        let params = ["wechat_union_id": unionId,
                      ] as [String : Any]
        Alamofire
            .request(url, method:.post, parameters: params)
            .responseJSON { (response) in
                guard response.result.isSuccess else {
                    SVProgressHUD.showError(withStatus: "加载失败...")
                    return
                }
                if let value = response.result.value {
                    printLog(response.result.value)
                    let dict = JSON(value)
                    let code = dict["statusCode"].intValue
                    guard code == RETURN_OK else {
                        if code == 6003{//手机号未绑定，需跳转绑定手机号
                            failure()
                        }
                        return
                    }
                    if let data = dict["data"].dictionaryObject{
                        let account = SDAccount(dict: data as [String:AnyObject])
                        success(account)
                    }
                }
        }
    }
}
